package org.openfidias.jdbase.model;

import java.io.Serializable;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.openfidias.jdbase.db.IRegister;
import org.openfidias.jdbase.db.Register;

/**
 *
 * @author atila
 */
public class Aluno extends Register implements Serializable, Comparable, IRegister {

   private static final long serialVersionUID = 1L;

   private int matricula;
   private String nome;
   private String login;
   
   /**
    * constants for limit character size
    */
   public static final int maxMatricula = 3;
   public static final int maxNome = 20;
   public static final int maxLogin = 10;

   public Aluno() {
      this.matricula = 0;
      this.nome = "";
      this.login = "";
      this.deleted = 0;
   }

   public Aluno(String login) {
      this.matricula = 0;
      this.nome = "";
      this.login = login;
      this.deleted = 0;
   }

   public Aluno(String login, String nome, int matricula) {
      this.matricula = matricula;
      this.nome = nome;
      this.login = login;
      this.deleted = 0;
   }

   public Aluno(String login, String nome, int matricula, short deleted) {
      this.matricula = matricula;
      this.nome = nome;
      this.login = login;
      this.deleted = deleted;
   }

   public String getLogin() {
      return StringUtils.trimToEmpty(this.login);
   }

   public void setLogin(String login) {
      this.login = login;
   }

   public int getMatricula() {
      return matricula;
   }

   public void setMatricula(int matricula) {
      this.matricula = matricula;
   }

   public String getNome() {
      return StringUtils.trimToEmpty(this.nome);
   }

   public void setNome(String nome) {
      this.nome = nome;
   }

   public String toLine() {
      StringBuilder builder = new StringBuilder();
      builder.append(new DecimalFormat("0").format(this.deleted));
      builder.append(":");
      builder.append( this.login );
      builder.append(":");
      builder.append( this.nome );
      builder.append(":");
      builder.append( new DecimalFormat("000").format(this.matricula) );
      builder.append(":");
      String line = StringUtils.rightPad(builder.toString(), size());
      return line;
   }

   @Override
   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("|");
      builder.append(StringUtils.rightPad(this.login, maxLogin));
      builder.append("|");
      builder.append(StringUtils.rightPad(this.nome, maxNome));
      builder.append("|");
      builder.append(new DecimalFormat("000").format(this.matricula));
      builder.append("      |");
      return builder.toString();
   }

   public static String headerTable() {
      StringBuilder builder = new StringBuilder();
      builder.append(rowSeparator());
      builder.append("\n|");
      builder.append(StringUtils.rightPad("Login", maxLogin));
      builder.append("|");
      builder.append(StringUtils.rightPad("Nome", maxNome));
      builder.append("|");
      builder.append("Matricula");
      builder.append("|\n");
      builder.append(rowSeparator());
      return builder.toString();
   }

   public static String rowSeparator() {
      StringBuilder builder = new StringBuilder();
      builder.append("+");
      builder.append(StringUtils.repeat("-", maxLogin));
      builder.append("+");
      builder.append(StringUtils.repeat("-", maxNome));
      builder.append("+");
      builder.append(StringUtils.repeat("-", 9)); // Matricula size
      builder.append("+");
      return builder.toString();
   }

   public int size() {
      int size = maxLogin + maxNome + maxMatricula + 5;// <<5>> 4 from ":" and 1 for "deleted"
//      size += login.size();
//      size += nome.size();
//      size += String.valueOf(matricula).size();
//      size += 3; // 2 caracteres ":", 1 "\n"
      return size;
   }

   @Override
   public boolean equals(Object obj) {
      if (!(obj instanceof Aluno)) {
         return false;
      }
      Aluno aluno = (Aluno) obj;
      return this.login.equals(aluno.getLogin());
   }

   @Override
   public int hashCode() {
      return new HashCodeBuilder().append(this.login).toHashCode();
   }

   public int compareTo(Object o) {
      Aluno aluno = (Aluno) o;
      return login.compareTo(aluno.getLogin());
   }

   public IRegister fromLine(String line) {
      String[] array = line.split(":");
      this.deleted = Short.parseShort(array[0]);
      this.login = array[1];
      this.nome = array[2];
      this.matricula = Integer.parseInt(array[3]);
      return this;
   }

   public boolean equals(String line) {
      String[] array = line.split(":");
      return isNotDeleted() && ( this.login.equals(array[1]) );
   }
}
